import type { FormRules } from "element-plus";
import { reactive } from "vue";
import { $t } from "@/plugins/i18n";
import { isNumLength, isPhoneNum, isValidLoginPwd } from "@/utils/tools";

/** 登录校验 */
const loginRules = reactive<FormRules>({
    username: [
        {
            required: true,
            message: $t("login.usernameReg"),
            trigger: "blur",
        },
    ],
    password: [
        {
            validator: (rule, value, callback) => {
                if (value === "") {
                    callback(new Error($t("login.passWordReg")));
                } else if (!isValidLoginPwd(value)) {
                    callback(new Error($t("login.passWordRuleReg")));
                } else {
                    callback();
                }
            },
            trigger: "blur",
        },
    ],
    captchaText: [
        {
            validator: (rule, value, callback) => {
                if (value === "") {
                    callback(new Error($t("login.verifyCodeReg")));
                } else {
                    callback();
                }
            },
            trigger: "blur",
        },
    ],
});

/** 手机登录校验 */
const phoneRules = reactive<FormRules>({
    phone: [
        {
            validator: (rule, value, callback) => {
                if (value === "") {
                    callback(new Error($t("login.phoneReg")));
                } else if (!isPhoneNum(value)) {
                    callback(new Error($t("login.phoneCorrectReg")));
                } else {
                    callback();
                }
            },
            trigger: "blur",
        },
    ],
    verifyCode: [
        {
            validator: (rule, value, callback) => {
                if (value === "") {
                    callback(new Error($t("login.verifyCodeReg")));
                } else if (!isNumLength(value, 6)) {
                    callback(new Error($t("login.verifyCodeSixReg")));
                } else {
                    callback();
                }
            },
            trigger: "blur",
        },
    ],
});

/** 忘记密码校验 */
const updateRules = reactive<FormRules>({
    phone: [
        {
            validator: (rule, value, callback) => {
                if (value === "") {
                    callback(new Error($t("login.phoneReg")));
                } else if (!isPhoneNum(value)) {
                    callback(new Error($t("login.phoneCorrectReg")));
                } else {
                    callback();
                }
            },
            trigger: "blur",
        },
    ],
    verifyCode: [
        {
            validator: (rule, value, callback) => {
                if (value === "") {
                    callback(new Error($t("login.verifyCodeReg")));
                } else if (!isNumLength(value, 6)) {
                    callback(new Error($t("login.verifyCodeSixReg")));
                } else {
                    callback();
                }
            },
            trigger: "blur",
        },
    ],
    password: [
        {
            validator: (rule, value, callback) => {
                if (value === "") {
                    callback(new Error($t("login.passWordReg")));
                } else if (!isValidLoginPwd(value)) {
                    callback(new Error($t("login.passWordRuleReg")));
                } else {
                    callback();
                }
            },
            trigger: "blur",
        },
    ],
});

export { loginRules, phoneRules, updateRules };
